<?php
namespace app\appclass\model;
use app\BaseModel;
use think\facade\Db;



class ClassCard extends BaseModel
{
    //定义表名
    protected $name = 'longbing_card_appclass_class_card';



    protected $append = [

        //'card_info'

    ];


//    /**
//     * @author chenniang
//     * @DataTime: 2021-05-14 10:34
//     * @功能说明:
//     */
//    public function getCardInfoAttr($value,$data){
//
//        if(!empty($data['card_id'])){
//
//            $card_model = new CardList();
//
//            $info = $card_model->dataInfo(['id'=>$data['card_id']]);
//
//            return $info;
//
//
//        }
//
//
//    }

    /**
     * @author chenniang
     * @DataTime: 2021-05-11 16:00
     * @功能说明:添加
     */
    public function dataAdd($data){

        $res = $this->insert($data);

        return $res;

    }


    /**
     * @author chenniang
     * @DataTime: 2021-05-11 16:03
     * @功能说明:编辑
     */
    public function dataUpdate($dis,$data){

        $res = $this->where($dis)->update($data);

        return $res;

    }


    /**
     * @author chenniang
     * @DataTime: 2021-05-11 16:04
     * @功能说明:详情
     */
    public function dataInfo($dis){

        $data = $this->where($dis)->find();

        return !empty($data)?$data->toArray():[];

    }


    /**
     * @author chenniang
     * @DataTime: 2021-05-11 16:05
     * @功能说明:
     */
    public function dataList($dis,$page=10){

        $data = $this->where($dis)->order('id desc')->paginate($page)->toArray();

        return $data;

    }


    /**
     * @author chenniang
     * @DataTime: 2021-05-12 10:08
     * @功能说明:添加
     */
    public function adminDataAdd($data,$class_id){

        $dis = [

            'class_id' => $class_id,
        ];

        $this->where($dis)->delete();

        if(!empty($data['class_card'])){

            foreach ($data['class_card'] as $k=> $v){

                $inset['uniacid'] = $data['uniacid'];

                $inset['card_id'] = $v['card_id'];

                $inset['class_id'] = $class_id;

                $inset['value']   = !empty($v['value'])?$v['value']:0;

                $this->insert($inset);

            }

        }

        return true;
    }


    /**
     * @author chenniang
     * @DataTime: 2021-05-14 15:22
     * @功能说明:获取可用的会员卡
     */
    public function canUseCard($date_info,$user_id,$num=1){

        $dis = [

            'b.user_id' => $user_id,

            'a.class_id'=> $date_info['class_id'],
            //已经开卡
            'b.status'  => 2
        ];

        $data = $this->alias('a')
                ->join('longbing_card_appclass_card_user b','a.card_id = b.card_id')
                ->join('longbing_card_appclass_card_list c','a.card_id = c.id')
                ->join('longbing_card_appclass_class_list d','b.store_id = d.store_id')
                ->where($dis)
                ->where('b.end_time','>',time())
                ->field('b.*,a.value as class_value,c.title as card_name')
                ->group('b.id')
                ->select()
                ->toArray();

        if(!empty($data)){

            foreach ($data as &$v){
                //会员卡时间限制
                $time_check = $this->checkTime($date_info['start_time'],$date_info['end_time'],$v['card_id']);
                //储值卡
                if($v['card_type']!=0){

                    $v['can_use'] = $v['end_time']>time()&&$v['true_value']>=$v['class_value']*$num&&$time_check==1?1:0;
                //次数卡
                }else{

                    $v['can_use'] = $v['end_time']>time()&&$time_check==1?1:0;

                }

                $v['end_time']= date('Y-m-d H:i',$v['end_time']);

            }

        }

        return $data;

    }


    /**
     * @author chenniang
     * @DataTime: 2021-06-18 17:50
     * @功能说明:用户
     */
    public function checkUserTime(){




    }


    /**
     * @author chenniang
     * @DataTime: 2021-05-18 09:36
     * @功能说明:核查会员卡对可用性
     */
    public function checkCard($date_info,$num,$card_id,$user_id){
        //检查会员卡限用次数
        $this->checkCardUseTimes($card_id,$num);

        $card_model = new CardUser();

        $record_model= new ClassRecord();

        $card_info = $card_model->dataInfo(['id'=>$card_id,'user_id'=>$user_id,'status'=>2]);

        if(empty($card_info)){

            $this->errorMsg('会员卡不存在');
        }
        //如果是期限卡 查看该时间段内有没有用过这张卡
        if($card_info['card_type']==0){

            $have_app1 = $record_model->where(['card_id'=>$card_id])->where('status','in',[1,3])->whereBetween('start_time',[$date_info['start_time'],$date_info['end_time']])->find();

            $have_app2 = $record_model->where(['card_id'=>$card_id])->where('status','in',[1,3])->whereBetween('end_time',[$date_info['start_time'],$date_info['end_time']])->find();

            if(!empty($have_app1)||!empty($have_app2)){

                $this->errorMsg('期限卡，一个时间段内不能重复预约');

            }

        }

        $dis = [

            'card_id' => $card_info['card_id'],

            'class_id'=> $date_info['class_id']
        ];

        $class_card_model = new ClassCard();
        //课程价格
        $value = $class_card_model->where($dis)->value('value');

        $use_value = $value*$num;

        if($card_info['true_value']<$use_value&&in_array($card_info['card_type'],[1,2])){

            if($card_info['card_type']==1){

                $this->errorMsg('次数不足');

            }else{

                $this->errorMsg('余额不足');
            }

        }

        if($card_info['end_time']<time()){

            $this->errorMsg('已过期限');

        }

        $check_time = $this->checkTime($date_info['start_time'],$date_info['end_time'],$card_info['card_id']);

        if($check_time==0){

            $this->errorMsg('该会员卡只能在指定时间内使用');

        }

        return [

            'value'     => $value,

            'true_value'=> $use_value,

            'card_type' => $card_info['card_type'],

            'card_true_value' => $card_info['true_value']

        ];

    }


    /**
     * @author chenniang
     * @DataTime: 2021-05-20 19:21
     * @功能说明:检查会员卡使用此时
     */
    public function checkCardUseTimes($card_id,$num){

        $card_model = new CardList();

        $card_user_model = new CardUser();

        $record_model = new ClassRecord();

        $day   = $record_model->cardUseCount($card_id,1);

        $week  = $record_model->cardUseCount($card_id,2);

        $month = $record_model->cardUseCount($card_id,3);

        $card_id = $card_user_model->where(['id'=>$card_id])->value('card_id');

        $card = $card_model->dataInfo(['id'=>$card_id]);

        if(!empty($card)){

            if($num+$day>$card['day_limit']&&$card['day_limit']>0){

                $this->errorMsg('该会员卡超出单日限用次数');
            }

            if($num+$week>$card['week_limit']&&$card['week_limit']>0){

                $this->errorMsg('该会员卡超出单周限用次数');
            }

            if($num+$month>$card['month_limit']&&$card['month_limit']>0){

                $this->errorMsg('该会员卡超出单月限用次数');
            }


        }

        return true;

    }



    /**
     * @author chenniang
     * @DataTime: 2021-05-17 11:22
     * @功能说明:
     */
    public function checkTime($start_time,$end_time,$card_id){

        $card_model = new CardList();

        $card = $card_model->dataInfo(['id'=>$card_id]);

        $s_week = date('w',$start_time);

        if($card['time_type']==1){

            $i = 0;

            $card_time_model = new CardTime();

            $dis = [

                'card_id' => $card_id,

                'week'    => $s_week
            ];

            $time_list = $card_time_model->where($dis)->select()->toArray();

            if(!empty($time_list)){

                foreach ($time_list as $value){
                    //可使用的开始时间
                    $c_start_time = strtotime($value['start_time']) - strtotime(date('Y-m-d ',time()))+strtotime(date('Y-m-d ',$start_time));
                    //可使用的结束时间
                    $c_end_time   = strtotime($value['end_time']) - strtotime(date('Y-m-d ',time()))+strtotime(date('Y-m-d ',$start_time));

                    $c_end_time   = $c_end_time>$c_start_time?$c_end_time:$c_end_time+86400;

                    if($c_start_time<=$start_time&&$c_end_time>=$end_time){

                        $i = 1;

                    }
                }

            }

        }else{

            $i = 1;
        }

        return  $i;
    }









}